package com.design_mode;

import java.util.*;

/**
 * Created by dustaple on 8/3/14.
 */
public class MediatorPattern {
    public static void main(String[] args) {
        Mediator mediator = new Mediator();
        LandlordA landlordA = new LandlordA(mediator);
        landlordA.rentOut();

        TenantA tenantA = new TenantA(mediator);
        tenantA.Rent();

        LandlordB landlordB = new LandlordB(mediator);
        landlordB.rentOut();

        TenantB tenantB = new TenantB(mediator);
        tenantB.Rent();
    }
}

abstract class AbstractMediator {
    Vector<AbstractLandlord> landlordList = new Vector<AbstractLandlord>();
    Vector<AbstractTenant> tenantList = new Vector<AbstractTenant>();

    public final void addLandlord(AbstractLandlord landlord) {
        landlordList.add(landlord);
        checkRequest(landlord);
    }

    public final void addTenant(AbstractTenant tenant) {
        tenantList.add(tenant);
        checkHouseInfo(tenant);
    }

    public final void checkHouseInfo(AbstractTenant tenant) {
        Iterator<AbstractLandlord> iterator = landlordList.iterator();
        while (iterator.hasNext()) {
            AbstractLandlord landlord = iterator.next();
            if (landlord.houseInfo().equals(tenant.getRequest())) {
                System.out.println("找到合适的房子，租房成功");
                notifyLandlord(landlord);
                break;
            }
        }
        System.out.println("暂时没有您所需要的房源，请等待，到时候我们会再通知您的。");
    }

    public final void checkRequest(AbstractLandlord landlord) {
        Iterator<AbstractTenant> iterator = tenantList.iterator();
        while (iterator.hasNext()) {
            AbstractTenant tenant = iterator.next();
            if (tenant.getRequest().equals(landlord.houseInfo())) {
                System.out.println("找到合适房客，租房成功");
                notifyTenant(tenant);
                break;
            }
        }
        System.out.println("暂时没有合适的房客，请等待，到时候我们会再通知您的。");
    }

    public final void notifyLandlord(AbstractLandlord landlord) {
        System.out.println(landlord.getClass().getName() + ",你的房子已被出租，欢迎下次惠顾。");
        landlordList.remove(landlord);
    }

    public final void notifyTenant(AbstractTenant tenant) {
        System.out.println(tenant.getClass().getName() + ",已经帮您租到你想要的房子了，欢迎下次惠顾。");
        tenantList.remove(tenant);
    }
}

class Mediator extends AbstractMediator {

}

abstract class AbstractLandlord {

    Mediator mediator;
    Integer houseInfo;

    public abstract void rentOut();

    protected final Integer houseInfo() {
        return houseInfo;
    }
}

class LandlordA extends AbstractLandlord {

    public LandlordA(Mediator mediator) {
        super.mediator = mediator;
    }

    @Override
    public void rentOut() {
        super.houseInfo = 1;
        System.out.println("三室一厅，5000/月.");
        mediator.addLandlord(this);
    }
}

class LandlordB extends AbstractLandlord {

    public LandlordB(Mediator mediator) {
        super.mediator = mediator;
    }

    @Override
    public void rentOut() {
        super.houseInfo = 2;
        System.out.println("2室一厅，3000/月.");
        mediator.addLandlord(this);
    }
}

abstract class AbstractTenant {
    Mediator mediator;
    Integer request;

    public final Integer getRequest() {
        return request;
    }

    public abstract void Rent();
}

class TenantA extends AbstractTenant {

    public TenantA(Mediator mediator) {
        super.mediator = mediator;
    }

    @Override
    public void Rent() {
        super.request = 2;
        System.out.println("希望租个2室一厅，每月3000以内.");
        mediator.addTenant(this);
    }
}


class TenantB extends AbstractTenant {

    public TenantB(Mediator mediator) {
        super.mediator = mediator;
    }

    @Override
    public void Rent() {
        super.request = 1;
        System.out.println("希望租个3室一厅，每月6000以内.");
        mediator.addTenant(this);
    }
}